package com.admission.web.admin;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.Special;
import com.admission.model.SpecialAttachment;
import com.admission.service.SpecialAttachmentManager;
import com.admission.service.SpecialManager;
import com.admission.util.NewFileName;
import com.admission.util.PageUtils;
import com.admission.web.CrudActionSupport;

@Namespace("/admin")
public class SpecialAttachmentAdminAction extends CrudActionSupport<SpecialAttachment> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String id;
	private SpecialAttachment entity;
	private File picture;
	private String pictureFileName;
	private Page<SpecialAttachment> page = new Page<SpecialAttachment>(
			PageUtils.DEFAULT_PAGE_SIZE);
	private SpecialAttachmentManager specialAttachmentManager;
	private String ids;
	private String keyword;
	private String spec_id;
	private List<Special> specialList;
	private SpecialManager specialManager;

	public SpecialAttachment getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		if (!StringUtils.isEmpty(keyword)) {
			PropertyFilter p = new PropertyFilter("LIKES_title", keyword);
			filters.add(p);
		}
		page.setOrder(Page.DESC);
		page.setOrderBy("inputTime");
		specialAttachmentManager.getByFilter(page, filters);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		specialList=specialManager.getAll();
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		String serverrealPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
		String subPath = null;
		try {
			if (picture != null) {
				subPath = new String();
				subPath = NewFileName.genNewFileName(pictureFileName, 0);
				FileUtils.copyFile(picture, new File(serverrealPath, subPath));
			}
			entity.setTitle(pictureFileName.substring(0,
					pictureFileName.lastIndexOf(".")));
			if(!StringUtils.isEmpty(spec_id))
				entity.setSpecial(specialManager.getSpecialById(spec_id));
			specialAttachmentManager.save(entity, subPath);
		} catch (Exception e) {
			this.addActionMessage("操作失败，请重新操作");
		}
		return list();
	}

	@Override
	public String delete() throws Exception {
		try {
			specialAttachmentManager.deleteById(ids);
		} catch (Exception e) {
			this.addActionMessage("操作失败，请重新操作");
		}
		return list();
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			entity = new SpecialAttachment();
		} else {
			entity = specialAttachmentManager.getById(id);
		}

	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public SpecialAttachment getEntity() {
		return entity;
	}

	public void setEntity(SpecialAttachment entity) {
		this.entity = entity;
	}

	public File getPicture() {
		return picture;
	}

	public void setPicture(File picture) {
		this.picture = picture;
	}

	public String getPictureFileName() {
		return pictureFileName;
	}

	public void setPictureFileName(String pictureFileName) {
		this.pictureFileName = pictureFileName;
	}

	public Page<SpecialAttachment> getPage() {
		return page;
	}

	public void setPage(Page<SpecialAttachment> page) {
		this.page = page;
	}

	public SpecialAttachmentManager getSpecialAttachmentManager() {
		return specialAttachmentManager;
	}

	@Autowired
	public void setSpecialAttachmentManager(
			SpecialAttachmentManager specialAttachmentManager) {
		this.specialAttachmentManager = specialAttachmentManager;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getSpec_id() {
		return spec_id;
	}

	public void setSpec_id(String spec_id) {
		this.spec_id = spec_id;
	}

	public List<Special> getSpecialList() {
		return specialList;
	}

	public void setSpecialList(List<Special> specialList) {
		this.specialList = specialList;
	}

	public SpecialManager getSpecialManager() {
		return specialManager;
	}
	@Autowired
	public void setSpecialManager(SpecialManager specialManager) {
		this.specialManager = specialManager;
	}

}
